package cn.scw.project.separate.service.impl;

import cn.scw.common.separate.SeparateMethod;
import cn.scw.project.separate.domain.SaRecord;
import cn.scw.project.separate.domain.SaUser;
import cn.scw.project.separate.domain.SeparateVo;
import cn.scw.project.separate.mapper.SaRecordMapper;
import cn.scw.project.separate.service.SaRecordService;
import cn.scw.project.separate.service.SaUserService;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SaRecordServiceImpl implements SaRecordService {

    @Resource
    private SaRecordMapper saRecordMapper;

    @Resource
    private SaUserService saUserService;

    @Override
    public Long save(SaRecord user) {
        if (user.getId() != null) saRecordMapper.update(user);
        else {
            user.setCreateDate(new Date());
            user.setIsDeleted(0);
            saRecordMapper.insert(user);
        }
        return user.getId();
    }

    @Override
    public void delete(Long id) {
        SaRecord user = new SaRecord();
        user.setIsDeleted(1);
        save(user);
    }

    @Override
    public void delete(Long[] id) {
        for (Long i : id) delete(i);
    }

    @Override
    public void clear(Long id) {
        saRecordMapper.delete(id);
    }

    @Override
    public List<Map<String, String>> reckon(Long orderId, String facId, BigDecimal money) {
        List<SaUser> list = saUserService.queryFacUser(facId);
        if (list == null || list.size() == 0) return null;
        List<Map<String, String>> mapList = list.parallelStream().map(user -> {
            Map<String, String> params = Maps.newHashMap();
            SaRecord saRecord = new SaRecord();
            saRecord.setUid(user.getId());
            saRecord.setFacCode(facId);
            saRecord.setOrderId(orderId);
            saRecord.setMoney(money);
            saRecord.setScale(user.getScale());
            saRecord.setGetMoney(money.multiply(user.getScale()).divide(BigDecimal.TEN.multiply(BigDecimal.TEN), 2, BigDecimal.ROUND_HALF_UP));
            saRecord.setState(0);
            saRecord.setCreateDate(new Date());
            saRecord.setIsDeleted(0);
            save(saRecord);
            params.put("amount", saRecord.getGetMoney().multiply(BigDecimal.TEN.multiply(BigDecimal.TEN)).intValue()+"");
            params.put("mercNo", user.getMercNo());
            params.put("openId", user.getOpenId());
            return params;
        }).collect(Collectors.toList());

        //新增默认分账
//       注释该默认分账用户，只需要实际参与分账得用户即可，liming by 20211126
//        Map<String, String> params = Maps.newHashMap();
//        BigDecimal decimal = money.multiply(SeparateMethod.DEFAULT_BL).divide(BigDecimal.TEN.multiply(BigDecimal.TEN), 2, BigDecimal.ROUND_UP);
//        params.put("amount", decimal.toString());
//        params.put("mercNo", SeparateMethod.DEFAULT_MERCNO);
//        if (decimal.compareTo(BigDecimal.ZERO) > 0) mapList.add(params);

        return mapList;
    }

    @Override
    public List<SaRecord> list(Map<String, Object> params) {
        return saRecordMapper.list(params);
    }

    @Override
    public void updateOrderState(Integer state, String orderId) {
        saRecordMapper.updateOrderState(state, orderId);
    }

    @Override
    public List<SeparateVo> queryUserRecord(Long orderId) {
        return saRecordMapper.queryUserRecord(orderId);
    }

    @Override
    public List<SeparateVo> querySeparateSuccRecord(Long orderId) {
        return saRecordMapper.querySeparateSuccRecord(orderId);
    }
}
